package com.zplus.managers.petservices.services.impls;

import com.zplus.managers.petservices.entities.Admin;
import com.zplus.managers.petservices.mappers.AdminMapper;
import com.zplus.managers.petservices.services.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServcieImpl implements AdminService
{
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public boolean register(Admin ad)
    {
        int i = adminMapper.insertAdmin(ad);
        return i > 0;
    }

    @Override
    public boolean deleteAdmin(Admin ad)
    {
        int i = adminMapper.dropAdmin(ad);

        return i > 0;
    }

    @Override
    public Admin checkLogin(Admin ad)
    {
        // if account exist
        Admin admin = adminMapper.queryAdminByPhoneNum(ad.getPhoneNum());
        if(admin == null)
            admin = adminMapper.queryAdminByEmail(ad.getEmail());

        if(admin == null)
            return null;
        // if secret pass

        if(ad.getPasswd().equals(admin.getPasswd()))
            return admin;

        return null;
    }

    @Override
    public Admin getAdminById(int id)
    {
        return adminMapper.queryAdminById(id);
    }

    @Override
    public List<Admin> getAllAdmin()
    {
        return adminMapper.queryAllAdmin();
    }

    @Override
    public Admin getAdminByNickname(String nickname)
    {
        // call mapper and get data
        return adminMapper.queryAdminByNickname(nickname);
    }

}
